package com.weixin.gong.example.jdk.bean;

import org.junit.Test;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

/**
 * 内省设置set,get
 * @author weixin.gong
 * @date 17-6-12 下午4:29
 */
public class IntrospectorExample {

    @Test
    public void systemOutPropertyNameIncludeParent() throws IntrospectionException {
        A a = new A();
        BeanInfo beanInfo = Introspector.getBeanInfo(A.class);
        for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
            System.out.println(propertyDescriptor.getName());
        }
    }

    @Test
    public void systemOutPropertyNameOnlySelf() throws IntrospectionException {
        A a = new A();
        BeanInfo beanInfo = Introspector.getBeanInfo(A.class, Object.class);
        for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
            System.out.println("name:" + propertyDescriptor.getName());
            System.out.println("displayName:" + propertyDescriptor.getDisplayName());
            System.out.println("shortDescription:" + propertyDescriptor.getShortDescription());
        }
    }

    static class A {
        private int a;

        public int getA() {
            return a;
        }

        public void setA(int a) {
            this.a = a;
        }
    }
}
